﻿Public Class UnitList
    Public minid As Int16 = 0
    Private Sub UnitList_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        UI.FormatDataGrid(DataGridView1)
        UI.FormatDataGrid(DataGridView2)
        loadministry()
        If minid <> 0 Then
            cbounit.SelectedValue = minid
        End If

    End Sub
    Private Sub loadministry()
        cbounit.ValueMember = "MinistryId"
        cbounit.DisplayMember = "MinistryName"
        cbounit.DataSource = cmdp.ExecuteDataTable("SELECT * FROM [Ministry]")
    End Sub
    Private Sub loademprole()
        cmdp.Parameters("@minid") = cbounit.SelectedValue
        DataGridView1.DataSource = cmdp.ExecuteDataTable("SELECT e.RoleId, r.RoleName,Count(e.RoleId) as [total] FROM Employee e INNER JOIN [Role] r ON e.RoleId=r.RoleId WHERE e.MinistryId=@minid  Group By e.RoleId,r.RoleName ")
    End Sub
    Private Sub loadempbypos()
        Dim ti = Now.Year
        cmdp.Parameters("@role") = DataGridView1.CurrentRow.Cells("RoleId").Value
        cmdp.Parameters("@minid") = cbounit.SelectedValue
        DataGridView2.DataSource = cmdp.ExecuteDataTable("SELECT e.EmpId,e.EmpName,p.PositionName," & ti & "-year(e.EmpDOB) as Age,(SELECT COUNT(*) FROM [Relative] r WHERE r.EmpId=e.EmpId And r.Active=true) as Kid FROM Employee e INNER JOIN [Position] p on p.PositionId=e.PositionId WHERE e.RoleId=@role AND MinistryId=@minid ORDER BY e.PositionId")
    End Sub
    Private Sub cbounit_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbounit.SelectedIndexChanged
        loademprole()
        DataGridView1.Focus()
    End Sub

    Private Sub DataGridView1_RowEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.RowEnter
        

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        MessageBox.Show(DataGridView1.CurrentRow.Cells("RoleId").Value)
    End Sub

    Private Sub DataGridView1_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView1.SelectionChanged
        'MessageBox.Show(DataGridView1.CurrentRow.Cells("RoleId").Value)
        loadempbypos()
    End Sub

    Private Sub DataGridView2_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView2.DoubleClick
        If Me.DataGridView2.SelectedRows.Count = 0 Then
            Exit Sub
        End If
        Dim row = Me.DataGridView2.SelectedRows(0)
        Dim frm As New ViewEmp
        frm.id = row.Cells("empid").Value
        'MessageBox.Show(row.Cells("empid").Value)
        frm.ShowDialog()
    End Sub

    Private Sub មលToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles មលToolStripMenuItem.Click
        DataGridView2_DoubleClick(sender, e)

    End Sub
End Class